import React, { Component } from 'react'
import './index.css'
import PropTypes from 'prop-types';//这里需要安装npm i prop-types
export default class Item extends Component {
    //对接受到的props进行：类型、必要性的限制
    static propTypes = {
        updateTodo: PropTypes.func.isRequired,
        delTodo: PropTypes.func.isRequired,
    }
    state = { mouse: false };//鼠标移入、移出
    //鼠标移入移出的回调
    handleMouse = (flag) => {
        return () => {
            this.setState({ mouse: flag })
        }
    }
    //勾选、取消勾选的回调
    handleChekc = (id) => {
        return (event) => {
            this.props.updateTodo(id, event.target.checked)
        }
    }
    //删除一个todo的回调
    handleDelete = (id) => {
        if (window.confirm('确定删除吗？')) {
            this.props.delTodo(id)
        }
    }
    render() {
        const { id, name, done } = this.props
        const { mouse } = this.state
        return (
            <li style={{ backgroundColor: mouse ? '#ddd' : 'white' }} onMouseLeave={this.handleMouse(false)} onMouseEnter={this.handleMouse(true)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleChekc(id)}    ></input>
                    <span>{name}</span>
                </label>
                <button className="btn btn-danger" onClick={() => { this.handleDelete(id) }} style={{ display: mouse ? 'block' : 'none' }}>删除</button>
            </li>
        )
    }
}
